package com.u.social.sale.service.impl;

import com.u.social.sale.entity.Product;
import com.u.social.sale.entity.ProductSku;
import com.u.social.sale.entity.ProductStock;
import com.u.social.sale.mapper.ProductMapper;
import com.u.social.sale.model.ProductSkuItem;
import com.u.social.sale.model.PublicProductDTO;
import com.u.social.sale.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.u.social.sale.service.IProductSkuService;
import com.u.social.sale.service.IProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alex Wu
 * @since 2025-03-05
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private IProductStockService productStockService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PublicProductDTO saveProduct(PublicProductDTO product) {
        // 保存商品
        Product savedProduct  = new Product();
        savedProduct .setName(product.getName());
        savedProduct .setDescription(product.getDesc());

        save(savedProduct);

        // 保存商品sku
        List<ProductSkuItem> skuItems = product.getSkuItems();

        for (ProductSkuItem skuItem : skuItems) {
            ProductSku productSku = new ProductSku();
            productSku.setProductId(savedProduct.getId());
            productSku.setSkuName(skuItem.getName());
            productSku.setSkuValue(skuItem.getValue());
            productSku.setPrice(skuItem.getPrice());
            // 保存商品sku
            productSkuService.save(productSku);

            ProductStock productStock = new ProductStock();
            productStock.setProductId(savedProduct.getId());
            productStock.setStock(skuItem.getStock());
            productStock.setSkuId(productSku.getId());
            // 保存商品库存
            productStockService.save(productStock);
        }


        // 缓存商品
        cacheProduct(savedProduct);
        return product;
    }

    @Override
    public List<Product> getHotProducts() {
        Object hotProducts = redisTemplate.opsForValue().get("hot_products");
        return hotProducts == null ? null : (List<Product>) hotProducts;
    }

    private void cacheProduct(Product savedProduct) {
        redisTemplate.opsForValue().set("product:" + savedProduct.getId(), savedProduct);
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void refreshHotProducts() {
        // 刷新热门商品
        List<Product> hotProducts = getHotProducts();
        redisTemplate.opsForValue().set("hot_products", hotProducts);
    }
}
